﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApi.Models;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class TestJsonPatchesController : ControllerBase
    {
        private readonly TodoContext _context;

        public TestJsonPatchesController(TodoContext context)
        {
            _context = context;
        }

        // GET: api/TestJsonPatches
        [HttpGet]
        public async Task<ActionResult<IEnumerable<TestJsonPatch>>> GetJsonPatches()
        {
            return await _context.JsonPatches.ToListAsync();
        }

        // GET: api/TestJsonPatches/5
        [HttpGet("{id}")]
        public async Task<ActionResult<TestJsonPatch>> GetTestJsonPatch(int id)
        {
            var testJsonPatch = await _context.JsonPatches.FindAsync(id);

            if (testJsonPatch == null)
            {
                return NotFound();
            }

            return testJsonPatch;
        }

        // PUT: api/TestJsonPatches/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutTestJsonPatch(int id, TestJsonPatch testJsonPatch)
        {
            if (id != testJsonPatch.Id)
            {
                return BadRequest();
            }

            _context.Entry(testJsonPatch).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TestJsonPatchExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/TestJsonPatches
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<TestJsonPatch>> PostTestJsonPatch(TestJsonPatch testJsonPatch)
        {
            _context.JsonPatches.Add(testJsonPatch);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetTestJsonPatch", new { id = testJsonPatch.Id }, testJsonPatch);
        }


        [HttpPatch]
        public async Task<IActionResult> GetTestJsonPatches([FromBody] JsonPatchDocument<TestJsonPatch> testJsonPatch)
        {
            if (testJsonPatch != null)
            {
                var model = new TestJsonPatch();
                testJsonPatch.ApplyTo(model, ModelState);
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                var testJsonPatch1 = await _context.JsonPatches.FindAsync(model.Id);
                if (testJsonPatch1 == null)
                {
                    return NotFound();
                }
                testJsonPatch1.Name = model.Name;
                testJsonPatch1.Description = model.Description;
                testJsonPatch1.Secret = model.Secret;
                testJsonPatch1.IsComplete = model.IsComplete;
                testJsonPatch1.UpdateTime = DateTime.Now;
                await _context.SaveChangesAsync();
                return Ok(testJsonPatch1);
            }
            return BadRequest();
        }


        // DELETE: api/TestJsonPatches/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteTestJsonPatch(int id)
        {
            var testJsonPatch = await _context.JsonPatches.FindAsync(id);
            if (testJsonPatch == null)
            {
                return NotFound();
            }

            _context.JsonPatches.Remove(testJsonPatch);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool TestJsonPatchExists(int id)
        {
            return _context.JsonPatches.Any(e => e.Id == id);
        }
    }
}
